<template>
  <div class="app-container dict-customFieldSetList-list-wrap">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="字段大类" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.field_category" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="clickAddbtn"
          v-hasPermi="['custom:customFieldSet:add']">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" size="mini" @click="handleDelete"
          v-hasPermi="['custom:customFieldSet:del']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-sort"
          size="mini"  
          @click="dialogVisible2=true">重排</el-button>
      </el-col>   -->
    </el-row>
    <el-table v-loading="loading" max-height="550" :data="dataList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handelEdit(scope.row)" v-hasPermi="['custom:customFieldSet:edit']">
            编辑</el-button>
        </template>
      </el-table-column>
      <el-table-column label="名称" prop="label"></el-table-column>
      <el-table-column label="ID" prop="code" />
      <el-table-column label="字段类型" prop="fieldType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.field_type" :value="scope.row.fieldType" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :title="ruleForm.id?'编辑字段集':'添加字段集'" :visible.sync="dialogVisible" width="600px">
      <div class="dialog-form-box">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
          <el-form-item label="字段大类" prop="type">
            <el-select v-model="ruleForm.type" placeholder="请选择" clearable style="width: 85%;">
              <el-option v-for="dict in dict.type.field_category" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="ID：" prop="code">
            <el-input v-model="ruleForm.code" style="width: 85%;"></el-input>
          </el-form-item>
          <el-form-item label="名称：" prop="label">
            <el-input v-model="ruleForm.label" style="width: 85%;"></el-input>
          </el-form-item>
          <el-form-item label="类型：" prop="fieldType">
            <el-select v-model="ruleForm.fieldType" placeholder="请选择" clearable style="width: 85%;">
              <el-option v-for="dict in dict.type.field_type" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="是否多个：" prop="dataType">
            <el-select v-model="ruleForm.dataType" placeholder="请选择" clearable style="width: 85%;">
              <el-option v-for="dict in dict.type.data_type" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="排序(拖动排序)" :visible.sync="dialogVisible2" width="600px">
      <div class="dialog-paixu-box">
        <ul>
          <vuedraggable class="wrapper" v-model="list">
            <transition-group>
              <li v-for="item in list" :key="item" class="item">
                <p>{{item}}</p>
              </li>
            </transition-group>
          </vuedraggable>
        </ul>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible2 = false">确认修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import vuedraggable from 'vuedraggable';
  import {
    customFieldSetList,
    customFieldSetAdd,
    customFieldSetEdit,
    customFieldSetBatch
  } from '@/api/system/dict/customerField.js'

  export default {
    name: "DictCustomFieldSetList",
    components: {
      vuedraggable
    },
    dicts: ['field_category', 'field_type', 'data_type'],
    data() {
      return {
        loading: false,
        multiple: true,
        queryParams: {
          pageNum: 1,
          pageSize: 30
        },
        dialogVisible: false,
        dialogVisible2: false,
        ruleForm: {
          type: []
        },
        rules: {
          type: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          code: [{
            required: true,
            message: "请输入编码",
            trigger: "change"
          }],
          label: [{
            required: true,
            message: "请输入名称",
            trigger: "change"
          }],
          fieldType: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          dataType: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
        },
        companyData: [],
        dataList: [],
        total: 0,
        list: [1, 2, 3, 4, 5, 6, 7]

      };
    },


    created() {
      this.getList()
    },
    methods: {

      getList() {
        this.loading = true;
        customFieldSetList(this.queryParams).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false;
        })
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSelectionChange(selection) {
        this.seletIds = selection.map(item => item.id)
        this.multiple = !selection.length
      },
      // 表单重置
      reset() {
        this.resetForm("ruleForm");
        this.ruleForm = {
          id: '',
          type: '',
          code: '',
          label: '',
          fieldType: '',
          dataType: ''
        };
      },
      //批量删除产品
      handleDelete() {
        let that = this
        let ids = ''
        this.seletIds.forEach((item, i) => {
          if (this.seletIds.length - 1 == i) {
            ids += item
          } else {
            ids += item + ','
          }
        });
        this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
          customFieldSetBatch({
            ids: ids
          }).then(res => {
            that.getList();
            that.$modal.msgSuccess("删除成功");
          });
        }).catch(function () {});
      },
      //点击添加按钮弹出表单
      clickAddbtn() {
        this.dialogVisible = true
        this.reset()

      },
      //点击编辑弹出编辑表单并赋值
      handelEdit(row) {
        this.dialogVisible = true
        this.reset()
        this.ruleForm.id = row.id
        this.ruleForm.type = row.type
        this.ruleForm.code = row.code
        this.ruleForm.label = row.label
        this.ruleForm.fieldType = row.fieldType
        this.ruleForm.dataType = row.dataType
      },
      //点击提交表单
      handelConfirm() {
        console.log(this.ruleForm)
        this.$refs["ruleForm"].validate(valid => {
          if (valid) {
            const params = {
              id: this.ruleForm.id || null,
              type: this.ruleForm.type,
              code: this.ruleForm.code,
              label: this.ruleForm.label,
              fieldType: this.ruleForm.fieldType,
              dataType: this.ruleForm.dataType,
            }
            if (this.ruleForm.id) {
              customFieldSetEdit(params).then(res => {
                this.$modal.msgSuccess("修改成功");
                this.dialogVisible = false;
                this.getList();
              })
            } else {
              customFieldSetAdd(params).then(res => {
                this.$modal.msgSuccess("添加成功");
                this.dialogVisible = false;
                this.getList();
              })
            }
          }
        })
      }
    }
  };

</script>

<style rel="stylesheet/scss" lang="scss">
  .dict-customFieldSetList-list-wrap {
    .dialog-paixu-box {
      ul {
        padding: 0;
        margin: 0;
      }

      li {
        list-style: none;
        cursor: move;
        height: 35px;
        line-height: 35px;
        border: solid 1px #ddd;
        padding-left: 10px;
        margin-top: -1px;

        p {
          margin: 0;
          padding: 0;
        }
      }
    }
  }

</style>
